#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<glob.h>
#include<string.h>
#define DELIMS " \t\n"
struct cmd_st
{
	glob_t glob_res;
};

static void parse(char *cmdbuff,struct cmd_st *res)
{
	char *tok;
	int i=0;
	while(1)
	{
		tok=strsep(&cmdbuff,DELIMS);
		if(tok==NULL)
			break;
		if(tok[0]=='\0')
			continue;
		glob(tok,GLOB_NOCHECK|GLOB_APPEND*i,NULL,&res->glob_res);
		i=1;
	}
}


int main()
{
	char *cmdbuf=NULL;
	size_t bufsize=0;
	struct cmd_st cmdres;
	
	pid_t pid;
	//glob_t globres;
	while(1)
	{
		printf("myshell~$");
		if(getline(&cmdbuf,&bufsize,stdin)<0)
				break;
		parse(cmdbuf,&cmdres);
	
		if(0)
		{

		}
		else
		{
			pid=fork();
			if(pid<0)
			{
				perror("fork()");
				return -1;
			}
			if(pid==0)
			{
				execvp(cmdres.glob_res.gl_pathv[0],cmdres.glob_res.gl_pathv);
				perror("execvp");
				return 1;

			}
			else
			{
				wait(NULL);
			}
		}
	}

	return 0;
}
